//
//  SHToolBarView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/28.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHToolBarView: UIView {
    
    // MARK: - 懒加载控件
    private lazy var retweetButton: UIButton = UIButton()
    // 评论
    private lazy var commentButton: UIButton = UIButton()
    // 赞
    private lazy var unlikeButton: UIButton = UIButton()
    
    // 供外界赋值viewModel
    var userInfoViewModel: SHUserInfoViewModel? {
        didSet {
            retweetButton.setTitle(userInfoViewModel?.repostsCountStr, forState: .Normal)
            commentButton.setTitle(userInfoViewModel?.commentsCountStr, forState: .Normal)
            unlikeButton.setTitle(userInfoViewModel?.attitudesCountStr, forState: .Normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置界面
    private func setupUI() {
        // 按钮
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        // 分割线
        let line1 = addChildLine()
        let line2 = addChildLine()
        
        // 添加约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(commentButton)
        }
       
        commentButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton.snp_trailing)
            make.width.equalTo(unlikeButton)
        }
        
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton.snp_trailing)
        }

        line2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton.snp_trailing)
        }    
    }
    
    // MARK: - 抽取分割线添加方法
    private func addChildLine()-> UIImageView {
        let img = UIImageView(imgName: "timeline_card_bottom_line_highlighted")
        addSubview(img)
        
        return img
    }
    
    // MARK: - 抽取按钮添加方法
    private func addChildButton(imageName: String, title: String)-> (UIButton) {
        let button:UIButton = UIButton(title: title, titleColor: UIColor.darkGrayColor(), font: 14, backgroundImgName: "timeline_card_bottom_background", imgName: imageName, target: nil, action:nil)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
            
        return button
    }

}
